package mdm.com.web.controller;

import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.com.web.common.AjaxResponse;
import mdm.com.web.common.BaseController;
import mdm.com.web.common.Constant;
import mdm.com.web.entity.Role;
import mdm.com.web.entity.RolePermission;
import mdm.com.web.entity.User;
import mdm.com.web.service.IRoleService;
import mdm.com.web.service.UserService;

@Controller
@RequestMapping(value = "role")
@Api(description = Constant.MODULE_ROLE)
public class RoleController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);

    private final IRoleService iRoleService;
    private final UserService userService;

    @Autowired
    public RoleController(IRoleService iRoleService, UserService userService) {
        this.iRoleService = iRoleService;
        this.userService = userService;
    }

    @ApiOperation(value = "新增职位", notes = "请求参数:<br>" + "cRoleName 职位名称")
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResponse add(@RequestBody Role role) {
        try {
            if (StringUtils.isEmpty(role.getcRoleName())) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE,"The role name has already existed", null);
            }
            if (iRoleService.selectByRoleName(role.getcRoleName()) != null) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE, "角色名已存在", null);
            }
            iRoleService.add(role);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, null);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }

    @ApiOperation(value = "删除职位", notes = "请求参数:<br>" + "cRoleId 职位id")
    @ResponseBody
    @RequestMapping(value = "/remove/{cRoleId}", method = RequestMethod.DELETE)
    public AjaxResponse remove(@PathVariable Integer cRoleId) {
        try {
            LOGGER.info("------------param:{}", cRoleId);
            if (cRoleId == null) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE, "role ID does not exist", null);
            }
            List<User> users = userService.selectByRoleId(cRoleId);
            if (users != null && users.size() > 0) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE,"", null);
            }
            iRoleService.remove(cRoleId);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, null);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }

    @ApiOperation(value = "修改职位", notes = "请求参数:<br>" + "cRoleId 职位id<br>" + "cRoleName 职位名称")
    @ResponseBody
    @RequestMapping(value = "/modify", method = RequestMethod.PUT)
    public AjaxResponse modify(@RequestBody Role role) {
        try {
            LOGGER.info("------------param:{}", role);
            if (StringUtils.isEmpty(role.getcRoleName())) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE, "角色名已存在", null);
            }
            Role role1 = iRoleService.selectByRoleName(role.getcRoleName());
            if (role1 != null && !Objects.equals(role.getcRoleId(), role1.getcRoleId())) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE, "", null);
            }
            iRoleService.modify(role);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, null);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }

    @ApiOperation(value = "职位查询", notes = "请求参数:<br>" + "pageIndex 页码<br>" + "limit 每页显示条数<br>" + "可选查询条件:<br>"
            + "cRoleName 职位名称<br>" + "startDate 开始时间<br>" + "endDate 结束时间")
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public AjaxResponse page(@RequestBody Role role, @RequestParam Integer pageIndex, @RequestParam Integer limit) {
        try {
            LOGGER.info("------------param:{}---{}---{}", role, pageIndex, limit);
            PageHelper.startPage(pageIndex, limit);
            List<Role> list = iRoleService.selectByCondition(role);
            for (int i = 0; i < list.size(); i++) {
                List<RolePermission> lp = iRoleService.selectByRoleId(list.get(i).getcRoleId());
                list.get(i).setLrolepermission(lp);
            }

            PageInfo result = new PageInfo<>(list);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, result);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }

    // add-jlq-2018-3-13
    @ApiOperation(value = "查询权限", notes = "请求参数:<br>" + "cRoleId 职位id")
    @ResponseBody
    @RequestMapping(value = "/select/{cRoleId}", method = RequestMethod.POST)
    public AjaxResponse selectByRoleId(@PathVariable Integer cRoleId) {
        try {
            LOGGER.info("------------param:{}", cRoleId);
            if (cRoleId == null) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE, "", null);
            }
            // List<User> users = iUserService.selectByRoleId(cRoleId);
            // if (users != null && users.size() > 0) {
            // return AjaxResponse.reponseBody(Constant.CHECK_CODE,
            // super.getMessage("role_check_exist_user"), null);
            // }
            List<RolePermission> list = iRoleService.selectByRoleId(cRoleId);
            PageInfo result = new PageInfo<>(list);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, result);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }
}
